/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.modelwalk');
(function() {

    ASC.loadBundle({
        locale: 'en_US',

        messages: {
            'net.autodata.modelwalk.label.please-select': 'Please Select',
            'net.autodata.modelwalk.label.modelYear': 'Year:',
            'net.autodata.modelwalk.label.make': 'Make:',
            'net.autodata.modelwalk.label.model': 'Model:',
            'net.autodata.modelwalk.label.trim': 'Trim:',
            'net.autodata.modelwalk.modelYear': 'Year',
            'net.autodata.modelwalk.make': 'Make',
            'net.autodata.modelwalk.model': 'Model',
            'net.autodata.modelwalk.trim': 'Trim'

        }
    });

    ASC.loadBundle({
        locale: 'en_CA',

        messages: {
            'net.autodata.modelwalk.label.please-select': 'Please Select',
            'net.autodata.modelwalk.label.modelYear': 'Year:',
            'net.autodata.modelwalk.label.make': 'Make:',
            'net.autodata.modelwalk.label.model': 'Model:',
            'net.autodata.modelwalk.label.trim': 'Trim:',
            'net.autodata.modelwalk.modelYear': 'Year',
            'net.autodata.modelwalk.make': 'Make',
            'net.autodata.modelwalk.model': 'Model',
            'net.autodata.modelwalk.trim': 'Trim'
        }
    });

    /**
    * A ModelWalk selection Panel with customizable views
    * @class ModelWalkPanel
    * @extends Ext.BoxComponent
    * @namespace net.autodata.modelwalk
    */
    var ModelWalk = Ext.extend(Ext.BoxComponent, {

        autoEl: {
            tag: 'div',
            cls: 'modelwalk-panel'
        },
        
        constructor: function (cfg)
        {
        	cfg = cfg || {};
        	Ext.apply(cfg, this);
        	this.initialConfig = cfg;
        	
        	ModelWalk.superclass.constructor.call(this, cfg);
        },
        
        /**
        * @property modelYear
        * @type net.autodata.modelwalk.ModelYearComboBox
        */
        modelYear: undefined,

        /**
        * @property make
        * @type net.autodata.modelwalk.MakeComboBox
        */
        make: undefined,

        /**
        * @property model
        * @type net.autodata.modelwalk.ModelComboBox
        */
        model: undefined,

        /**
        * @property trim
        * @type net.autodata.modelwalk.TrimComboBox
        */
        trim: undefined,

        /**
        * Defines how the ComboBoxes within the ModelWalk interact with each other
        * @property dependencyMap
        */
        dependencyMap: {
            modelYear: {
                id: 'modelYear',
                dependents: 'make',
                cfg: {
                    xtype: 'acompare-mw-modelyearcombo',
                    emptyText: ASC.message('net.autodata.modelwalk.label.please-select'),
                    width: 200
                }
            },

            make: {
                id: 'make',
                dependents: 'model',
                requiredParams: 'modelYear',
                cfg: {
                    xtype: 'acompare-mw-makecombo',
                    emptyText: ASC.message('net.autodata.modelwalk.label.please-select'),
                    width: 200
                }
            },

            model: {
                id: 'model',
                dependents: 'trim',
                requiredParams: ['modelYear', 'makeCode'],
                cfg: {
                    xtype: 'acompare-mw-modelcombo',
                    emptyText: ASC.message('net.autodata.modelwalk.label.please-select'),
                    width: 200
                }
            },

            trim: {
                id: 'trim',
                dependents: undefined,
                requiredParams: ['modelYear', 'makeCode', 'modelCode'],
                cfg: {
                    xtype: 'acompare-mw-trimcombo',
                    emptyText: ASC.message('net.autodata.modelwalk.label.please-select'),
                    width: 200
                }
            }
        },

        /**
        * @method constructor
        */
        constructor: function() {
            ModelWalk.superclass.constructor.apply(this, arguments);
        },

        /**
        * @method initComponent
        */
        initComponent: function() {
            ModelWalk.superclass.initComponent.apply(this, arguments);

            Ext.iterate(this.dependencyMap, function(key, value) {
                var comboId = value.id || key;
                value.cfg = value.cfg || {};
                value.cfg.emptyText = ASC.message('net.autodata.modelwalk.label.please-select');
                var combo = new Ext.create(value.cfg);
                this[comboId] = combo;
            }, this);

            var view = this.getView();
            view.init(this);
        },

        /**
        * @method clearAll
        */
        clearAll: function() {
            this.modelYear.clearValue();
        },

        /**
        * Returns the ModelWalkPanel's View object.
        * @return {Object} the View
        */
        getView: function() {
            if (!this.view) {
                this.view = new net.autodata.modelwalk.ModelWalkView(this.viewConfig);
            }

            return this.view;
        },

        /**
        * @method onRender
        * @private
        */
        onRender: function() {
            ModelWalk.superclass.onRender.apply(this, arguments);

            var view = this.getView();
            view.render();

            Ext.iterate(this.dependencyMap, function(key, value) {
                var combo = this[key];
                if (combo === undefined) {
                    return;
                }

                combo.getStore().requiredParams = value.requiredParams;

                var dependents = [];
                Ext.each(value.dependents, function(dependent) {
                    var dependentCombo = this[dependent];
                    if (dependentCombo) {
                        dependents.push(dependentCombo);
                    }
                }, this);

                if (dependents.length > 0) {
                    combo.dependents = dependents;
                }

            }, this);

            ASC.log('ModelWalk->onRender');
        }
    });

    net.autodata.modelwalk.ModelWalkPanel = ModelWalk;
})();